package com.lw.sbdemo2.web;

import com.lw.sbdemo2.common.base.BaseController;
import com.lw.sbdemo2.common.utils.CodeUtil;
import com.lw.sbdemo2.common.utils.IdUtils;
import com.lw.sbdemo2.common.utils.ValidUtil;
import com.lw.sbdemo2.common.vo.PageInfo;
import com.lw.sbdemo2.common.web.anno.Required;
import com.lw.sbdemo2.common.web.anno.ResultController;
import com.lw.sbdemo2.entity.TkTeacherClassStudent;
import com.lw.sbdemo2.entity.TkUserInfo;
import com.lw.sbdemo2.mapper.TkUserInfoMapper;
import com.lw.sbdemo2.service.TkTeacherClassStudentService;
import com.lw.sbdemo2.service.TkUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * <p>标题: 用户信息表服务 - 请求控制层</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2019</p>
 * <p>公司: 山人行工作室</p>
 *
 * @version: 1.0
 * @author: LinWang
 * @date 2019-4-14
 */
@ResultController
@Api(description = "TkUserInfoController", tags = {"用户信息表服务"})
@RequestMapping(value = "/tkUserInfo")
public class TkUserInfoController extends BaseController {

    /**
     * 用户信息表服务
     */
    @Autowired
    private TkUserInfoService tkUserInfoService;

    @Autowired
    private TkUserInfoMapper tkUserInfoMapper;

    /**
     * 教师记录表服务
     */
    @Autowired
    private TkTeacherClassStudentService tkTeacherClassStudentService;

    @ApiOperation(value = "获取用户信息表列表")
    @GetMapping("")
    public List<TkUserInfo> findList(TkUserInfo tkUserInfo) {
        return tkUserInfoService.findList(tkUserInfo);
    }

    @ApiOperation(value = "获取所有用户信息表列表")
    @GetMapping("/findAllList")
    public List<TkUserInfo> findAllList(TkUserInfo tkUserInfo) throws Exception {
        return tkUserInfoService.findAllList(tkUserInfo);
    }

    @ApiOperation(value = "获取用户信息表列表")
    @GetMapping("/findListGrade")
    public List<TkUserInfo> findListGrade(String query,TkUserInfo tkUserInfo) {
        return tkUserInfoService.findListGrade(query,tkUserInfo);
    }

    @ApiOperation(value = "获取用户信息表列表（分页）", response = PageInfo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "关键词", dataType = "string"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
    })
    @GetMapping(value = "/page")
    public PageInfo<TkUserInfo> page(String query, @Required("pageNum") PageInfo pageInfo, TkUserInfo tkUserInfo) {
        return tkUserInfoService.findListPage(query, pageInfo, tkUserInfo);
    }

    @ApiOperation(value = "获取用户信息表详细信息", notes = "根据url的id来获取用户信息表详细信息")
    @ApiImplicitParam(name = "id", value = "用户信息表ID", required = true, dataType = "String")
    @GetMapping("/{id}")
    public TkUserInfo get(@PathVariable String id) {
        return tkUserInfoService.getById(id);
    }

    @ApiOperation(value = "创建用户信息表")
    @PostMapping("/insert")
    public void insert(TkUserInfo tkUserInfo) {
        tkUserInfo.setCreatedTime(new Date());
        tkUserInfo.setUserId(IdUtils.uuid());
        tkUserInfoService.insert(tkUserInfo);
    }

    @ApiOperation(value = "更新用户信息表详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户信息表ID", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/update")
    public void update(TkUserInfo tkUserInfo) throws Exception {

        tkUserInfo.setModifiedTime(new Date());
        //有绑定教师则更新对应的学校、年级、班级

        if(ValidUtil.isNotEmptyAndNull(tkUserInfo.getUserName())){
            String code = CodeUtil.cn2FirstSpell(tkUserInfo.getUserName());
            if (StringUtils.isNotEmpty(code)) {
                code = code.substring(0,1);
            }
            tkUserInfo.setSpellCode(code);
        }

        if (!ValidUtil.isNotEmptyAndNull(tkUserInfo.getTeacherId1())) {
            if (ValidUtil.isNotEmptyAndNull(tkUserInfo.getTeacherId()))  {
                TkTeacherClassStudent tkTeacherRecord = tkTeacherClassStudentService.getById(tkUserInfo.getTeacherId());
                if (tkTeacherRecord != null) {
                    tkTeacherRecord.setTeacherId(tkUserInfo.getTeacherId1());
                    // tkUserInfo.setTeacherId(tkTeacherRecord.getTeacherId());
                }
                tkTeacherClassStudentService.update(tkTeacherRecord);
            }
        } else {
            //后台管理跟换老师时，修改学生dataId
            TkTeacherClassStudent tkTeacher = TkTeacherClassStudent.builder().teacherId(tkUserInfo.getTeacherId1()).build();
            List<TkTeacherClassStudent> tkTeacherInfo = tkTeacherClassStudentService.findAllList(tkTeacher);
            if (tkTeacherInfo != null && tkTeacherInfo.size() > 0) {
                tkUserInfo.setTeacherId(tkTeacherInfo.get(0).getDataId());
            }
        }

        tkUserInfoService.update(tkUserInfo);
    }

    @ApiOperation(value = "更新用户信息表详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户信息表ID", required = true, paramType = "query", dataType = "String")
    })
    @PostMapping("/updateTeacher")
    public void updateTeacher(String userId) {
        tkUserInfoMapper.updateTea(userId);
    }

    @ApiOperation(value = "删除用户信息表", notes = "删除用户信息表")
    @ApiImplicitParam(name = "id", value = "用户信息表ID", required = true, paramType = "query", dataType = "String")
    @PostMapping("/delete")
    public void deleteTkUserInfo(TkUserInfo tkUserInfo) {
        tkUserInfoService.delete(tkUserInfo);
    }

}
